<?php

  class fsock{
  	
  	private $host;
  	private $method;
  	private $str;
  	private $sessid;
  	private $sessionname;
  	private $login_page; 
  	private $content;

  	
  	public function init_var($host,$method,$str,$si,$sn,$loginpg){
  		$this->set_host($host);
  		$this->set_method($method);
  		$this->set_sessid($si);
  		$this->set_sn($sn);
  		$this->set_str($str);
  		$this->set_lp($loginpg);
  	}
  	  	
  	private function set_host($hostname){
  		$this->host = $hostname;
  	}
  	
    private function set_method($met){
  		$this->method = $met;
  	}
  	
  	private function set_str($t){
  		$this->str = $t;
  	}
  	
  	private function set_sessid($sid){
  		$this->sessid = $sid;
  	}
  	
  	private function set_sn($sn){
  		$this->sessionname = $sn;
  	}
  	
 	private function set_lp($lp){
  		$this->login_page = $lp;
  	}
  	
  	public function fsock($host,$method,$str,$si,$sn,$loginpg){
  		$this->init_var($host,$method,$str,$si,$sn,$loginpg);
  	}
  	
  	private function GetWebContent($host, $method, $str, $sessid = '',$sessionname){
  		
  		$ip = gethostbyname($host); 
	    $fp=@fsockopen($ip,80,$errno,$errstr,60); 
	    if (!$fp) return; 
	    
	    
	    fputs($fp, "$method\r\n"); 
	    fputs($fp, "Host: $host\r\n"); 
	    if (!empty($sessid)) 
	    { 
	        fputs($fp, "Cookie: $sessionname=$sessid; path=/;\r\n"); 
	    } 
	    if ( substr(trim($method),0, 4) == "POST") 
	    { 
	        fputs($fp, "Content-Length: ". strlen($str) . "\r\n"); //  别忘了指定长度 
	    }
		fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n"); 
		fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)\r\n");//add by Ew 071012
		fputs($fp, "Connection: Keep-Alive\r\n\r\n");
	    if ( substr(trim($method),0, 4) == "POST") 
	    { 
	        fputs($fp, $str."\r\n"); 
	    } 
	    while(!feof($fp)) 
	    { 
	        $response .= fgets($fp); 
	    } 
	    $hlen = strpos($response,"\r\n\r\n"); // LINUX下是 "\n\n" 
	    $header = substr($response, 0, $hlen); 
	    $entity = substr($response, $hlen + 4); 
	    
	    //echo $header;
	    //echo "\r\n\r\n\r\n\r\n";
	    
	    $match_str = '/' . $sessionname . '=([0-9a-z]+);/i';
	    
	    if ( preg_match($match_str, $header, $matches))
	    {
	        $a[$sessionname] = $matches[1]; 
	    }
	    
	    if ( preg_match('/Location: ([0-9a-z\_\?\=\&\#\.]+)/i', $header, $matches)) 
	    {
	        $a['location'] = $matches[1]; 
	    }
	    
	    $a['content'] = $entity;     
	    fclose($fp); 
	    return $a; 
  	}
  	
  	public function get_ses(){
  		$sd = $this->GetWebContent($this->host,$this->method,$this->str,'',$this->sessionname);
  		$this->set_sessid($sd[$this->sessionname]);
  		return $sd[$this->sessionname];
  	}
  	
  	public function get_con($met,$str){
  		$sd = $this->GetWebContent($this->host,$met,$str,$this->sessid,$this->sessionname);
  		$this->content = $sd['content'];
  		$this->set_str($str);
  		return $this->content;
  	}
  	
  }
  
  
  
  
  class webcontent{
  	
  	private $host;
  	private $method;
  	private $str;
  	private $sessid;
  	private $sessionname;
  	private $login_page; 
  	private $content;
  	private $path;
  	private $elsesky;

  	public function webcontent($host,$method,$str,$si,$sn,$loginpg){
  		$this->init_var($host,$method,$str,$si,$sn,$loginpg);
  	}
  	  	
  	public function init_var($host,$method,$str,$si,$sn,$loginpg){
  		$this->set_host($host);
  		$this->set_method($method);
  		$this->set_sessid($si);
  		$this->set_sn($sn);
  		$this->set_str($str);
  		$this->set_lp($loginpg);
  	}
  	  	
  	private function set_host($hostname){
  		$this->host = $hostname;
  	}
  	
    private function set_method($met){
  		$this->method = $met;
  	}
  	
  	private function set_str($t){
  		$this->str = $t;
  	}
  	
  	private function set_sessid($sid){
  		$this->sessid = $sid;
  	}
  	
  	private function set_sn($sn){
  		$this->sessionname = $sn;
  	}
  	
 	private function set_lp($lp){
  		$this->login_page = $lp;
  	}
  	
  	public function set_path($path){
  		$this->path = $path;
  	}
  	
  	public function get_webcontent(){
  		$fk = new fsock($this->host,$this->method,$this->str,$this->sessid,$this->sessionname,$this->login_page);
  		return $fk->get_con("GET " . $this->path . " HTTP/1.1",'');
  	}
  	
  	public function do_login(){
  		$fk = new fsock($this->$host,$this->$method,$this->$str,'',$this->$sn,$this->$loginpg);
  		$sid = $fk->get_ses();
  		$this->set_sessid($sid);
  		return $sid;
  	}
  }
  
  $login_page = 'forum/login.php?do=login';
  $sessionname = 'hongfiresessionhash';
  $host = "www.hongfire.com";
  $str = "vb_login_username=elsesky&vb_login_password=&s=&securitytoken=guest&do=login&vb_login_md5password=f3e9ab694537b24c71032d23449da36d&vb_login_md5password_utf=f3e9ab694537b24c71032d23449da36d";
  
  $test = new webcontent($host,"POST /$login_page HTTP/1.1",$str,'',$sessionname,$login_page);
  $test->set_path("/cg");
  $test->do_login();
  echo $test->get_webcontent();
  
  
  exit;
  
  
  //登录
  $elsesky = new fsock($host,"POST /$login_page HTTP/1.1",$str,'',$sessionname,$login_page);
  
  $elsesky->get_ses();
  //获取
  echo $elsesky->get_con("GET /cg HTTP/1.1",'');
  
  
  
  
  
?>


